<?php
class Category_Form_QuickSearchResidentialForm extends Ac_Form
{
    private $_preview_link = '';

    private $_category;
    private $_adid;

    protected $elementButtonDecorators = array(
        'ViewHelper',
        'Errors',
    );

    protected $elementDecorators = array(
        'ViewHelper',
        'Errors',
    );

    protected $formDecorators = array(
        'PrepareElements',
        array('ViewScript', array('viewScript' => 'index/customization/residential.phtml'))
    );

    protected $subFormDecorators = array(
        'FormElements',
        array('Description', array('placement' => 'prepend')),
        'fieldset',
    );

    protected $spanDecorators = array(
        'ViewHelper',
        'Errors',
        array('Label', array('placement' => 'prepend')),
        array(array('row' => 'HtmlTag'), array('tag' => 'span')),
    );

    protected $displayGroupDecorators = array(
        'FormElements',
        array('HtmlTag', array('tag' => 'p', 'class' => 'price radio text')),
        array('Description', array('placement' => 'prepend', 'tag' => 'label')),
    );

    protected $elementCheckBoxDecorators = array(
        'ViewHelper',
        'Errors',
        array('Label', array('placement' => 'APPEND')),
        array(array('row' => 'HtmlTag'), array('tag' => 'span')),
    );

    protected $elementMultiCheckBoxDecorators = array(
        'ViewHelper',
        'Errors',
        array('Label', array('placement' => 'APPEND')),
    );

    protected $elementRadioDecorators = array(
        'ViewHelper',
        'Errors',
        array('Label', array('placement' => 'APPEND')),
        array(array('row' => 'HtmlTag'), array('tag' => 'span')),
    );

    public function __construct($options = array())
    {
        $this->_preview_link = !empty($options['preview_link']) ? $options['preview_link'] : '';
        $this->_category = !empty($options['category']) ? $options['category'] : array();
        $this->_adid = !empty($options['adid']) ? $options['adid'] : array();
        parent::__construct($options);
    }
    
    public function init()
    {
        parent::init();

        $this->setMethod('get');
        $this->setAction($this->getView()->geturl('regular' ,'search'));
        
        $element = new Zend_Form_Element_Hidden('adid');
        $element->setValue((string) $this->_adid);
        $element->setDecorators(array(
                'ViewHelper'
            )
        );
        $this->addElement($element);


        //Category
        $this->addElement('hidden', 'small_category_list', array(
            'filters'    => array('StringTrim', 'StringToLower'),
            'validators' => array(
                'Int'
            ),
            'value' => 'residential',
            'disableLoadDefaultDecorators' => true,
            'decorators' => $this->elementDecorators,
            'required'   => true,
        ));
     
        //Sell By
        /*
        $this->addElement('radio', 'agency_owner', array(
            'filters'    => array('StringTrim', 'StringToLower'),
            'validators' => array(
                'Int'
            ),
            'multiOptions' => array(
                'Agency' => 'Agency',
                'Owner' => 'Owner',
            ),
            'disableLoadDefaultDecorators' => true,
            'decorators' => $this->elementRadioDecorators,
            'required'   => true,
        ));
        */

        //Want to
        $this->addElement('radio', 'wantto', array(
            'filters'    => array('StringTrim', 'StringToLower'),
            'validators' => array(
                'Int'
            ),
            'multiOptions' => array(
                'Buy' => 'Buy',
                'Rent' => 'Rent',
            ),
            'separator' => ' ',
            'disableLoadDefaultDecorators' => true,
            'decorators' => $this->elementDecorators,
            'required'   => true,
        ));

        // -> Price
        /*
        $this->addElement('jqslider', 'price', array(
                    'jQueryParams' => array(
                        'min'      => 0,
                        'max'      => 20000000,
                        'values'   => array('0', '20000000'),
                        'currency' => '$HKD',
                        'range'    => true,
                        'step'     => 500,
                        ),
                    'decorators' => array(
                            'Errors',
                            'UiWidgetElement',
                            //array('Label', array('escape' => false)),
                            //array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => 'ac-slider-values')),
                    )
                )
        );
*/
        $this->addElement('textRange', 'price', array(
                    'value'       => array(),
                    'class'       => array('class' => 'text-range'),
                    'decorators'  => array(
                            'Errors',
                            'ViewHelper',
                    )
                )
        );
        
        $this->addElement('checkbox', 'sole_agency', array(
            'validators' => array(
            ),
            'disableLoadDefaultDecorators' => true,
            'label' => 'Sole Agency',
            'decorators' => array(
                'ViewHelper',
                'Errors',
                array('Label', array('placement' => 'append')),
            ),
            'required'   => true,
        ));

        /**********************************************************
         *                          AGENT
         *********************************************************/
        /*
        $this->addElement('text', 'agent_name', array(
            'filters'    => array('StringTrim', 'StringToLower'),
            'validators' => array(
            ),
            'disableLoadDefaultDecorators' => true,
            'decorators' => $this->elementDecorators,
            'required'   => true,
        ));
        $this->addElement('text', 'agent_id', array(
            'filters'    => array('StringTrim', 'StringToLower'),
            'validators' => array(
            ),
            'disableLoadDefaultDecorators' => true,
            'decorators' => $this->elementDecorators,
            'required'   => true,
        ));

        $this->addElement('text', 'agent_phone', array(
            'filters'    => array('StringTrim', 'StringToLower'),
            'validators' => array(
                'int',
            ),
            'disableLoadDefaultDecorators' => true,
            'decorators' => $this->elementDecorators,
            'required'   => true,
        ));

        $this->addElement('text', 'agent_email', array(
            'filters'    => array('StringTrim', 'StringToLower'),
            'validators' => array(
                'EmailAddress',
            ),
            'disableLoadDefaultDecorators' => true,
            'decorators' => $this->elementDecorators,
            'required'   => true,
        ));
*/
        /**********************************************************
         *                          LOCATION
         *********************************************************/
        $areas = array();
        /*
        $locs = Model_Location::all(array('path' => array('$size' => 1)))->sort(array('path' => 1));
        foreach($locs as $k => $loc) {
            $areas[(string)$loc->_id] = $loc->name;
        }
        $selected_area = empty($_POST['area']) ? 0 : $_POST['area'];
        if (empty($selected_area)) {
            reset($areas);
            $selected_area = key($areas);
        }
         *
         */
        $locations = array('All');
        $locs = Model_Location::all()->sort(array('path' => 1));
        $parent = '';
        foreach($locs as $k => $loc) {

            if (count($loc->path) == 1) {
                $parent = $loc->name;
                $locations[$loc->name] = array();
            } else {

                //$parent = (string) $loc->path[0];
                $tmp    = !empty($locations[(string) $parent]) ? $locations[(string) $parent] : array();

                $tmp[$k]         = (string) $loc->name;
                //echo '<pre>';
                //print_r($tmp);
                $locations[(string) $parent] = $tmp;
            }
        }
/*
        $this->addElement('select', 'area', array(
            'filters'    => array('StringTrim'),
            'validators' => array(
                'Alnum',
                array('StringLength', false, 1),
            ),
            'multiOptions' => $locations,
            'disableLoadDefaultDecorators' => true,
            'decorators' => $this->elementDecorators,
            'required'   => false,
        ));
*/
        //District

        $this->addElement('select', 'area_l2', array(
            'filters'    => array('StringTrim'),
            'validators' => array(
                'Alnum',
                array('StringLength', false, 24),
            ),
        //    'multiOptions' => Model_Location::getDistrict($selected_area),
            'multiOptions' => $locations,
            'disableLoadDefaultDecorators' => true,
            'decorators' => $this->elementDecorators,
            'required'   => false,
        ));
/*
        // -> Bedrooms
        $this->addElement('jqslider', 'bedroom', array(
                    'jQueryParams' => array(
                        'min'      => 0,
                        'max'      => 10,
                        'values'   => array('0', '10'),
                        'currency' => '$HKD',
                        'range'    => true,
                        'step'     => 1,
                        ),
                    'decorators' => array(
                            'Errors',
                            'UiWidgetElement',
                    )
                )
        );
*/
        // -> Size
        $this->addElement('jqslider', 'size', array(
                    'jQueryParams' => array(
                        'min'      => 0,
                        'max'      => 5000,
                        'values'   => array('0', '5000'),
                        'currency' => 'sqft',
                        'range'    => true,
                        'step'     => 500,
                        'change'   => 'refineSearch($(this))',
                        ),
                    'decorators' => array(
                            'Errors',
                            'UiWidgetElement',
                    )
                )
        );
/*
        // -> Building age
        $this->addElement('jqslider', 'building_age', array(
                    'jQueryParams' => array(
                        'min'      => 0,
                        'max'      => 50,
                        'values'   => array('0', '50'),
                        'range'    => true,
                        'step'     => 1,
                        ),
                    'decorators' => array(
                            'Errors',
                            'UiWidgetElement',
                    )
                )
        );

        // --> Keyword
        $this->addElement('text', 'keyword', array(
            'filters'    => array('StringTrim'),
            'validators' => array(
                //'EmailAddress',
            ),
            'decorators' => $this->elementDecorators,
            'required'   => false,
        ));
*/
        $this->addElement('button', 'v', array(
            'required'    => false,
            'ignore'      => true,
            'type'        => 'submit',
            'label'       => 'Search',
            'decorators'  => $this->elementButtonDecorators,
        ));

    }

    public function __toString()
    {
        $str = '
            $j(document).ready(function() {
                $j("#area_l1").change(function (event) {
                    event.stopImmediatePropagation();
                    $j.get("/search/get-attribute", { c:"area_l1", f:"area_l2", id: $j("#area_l1").val() },
                        function(data) {
                            $j("#area_l2").empty();
                            $j.each(data, function (index, value) {
                                $(document.createElement("option"))
                                    .html(value)
                                    .attr("value", index)
                                    .appendTo($("#area_l2"));
                            });
                        });
               });
           });
        ';
        $this->view->JQuery()->addJavascript($str);
        return parent::__toString();
    }
}
